﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Text;
using DevExpress.XtraEditors;
using System.Windows.Forms;

namespace INHOADON_TAMVAN
{
    class SQLServerUtility
    {
        public static void SaoLuuCoSoDuLieu(string tenCoSoDuLieu, string tenDuongDan)
        {
            //Test connection
            SqlConnection myConnection = new SqlConnection(SystemInfo.pub_ConnString);

            try
            {
                myConnection.Open();

                String strBackUp;
                strBackUp = "BACKUP DATABASE " + tenCoSoDuLieu + " TO DISK = '" +
                    tenDuongDan + "'";

                SqlCommand sqlCommandObj = new SqlCommand(strBackUp, myConnection);
                sqlCommandObj.ExecuteNonQuery();

                XtraMessageBox.Show("Sao lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception e)
            {
                XtraMessageBox.Show("Sao lưu không thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                myConnection.Close();
            }
        }

        public static void PhucHoiCoSoDuLieu(string tenCoSoDuLieu, string tenDuongDan)
        {
            //Test connection
            SqlConnection myConnection = new SqlConnection(SystemInfo.pub_ConnString);
            SqlCommand sqlCommandObj = new SqlCommand();

            try
            {
                myConnection.Open();

                sqlCommandObj.Connection = myConnection;

                String strPhucHoi;

                // Cơ sở dữ liệu đã tồn tại -> Xóa
                if (CoSoDuLieuCoTonTai(tenCoSoDuLieu, myConnection))
                {
                    myConnection.ChangeDatabase("Master");

                    sqlCommandObj.Connection = myConnection;

                    String strAlterDatabase = " ALTER DATABASE " + tenCoSoDuLieu + " SET SINGLE_USER WITH ROLLBACK IMMEDIATE";
                    sqlCommandObj.CommandText = strAlterDatabase;
                    sqlCommandObj.ExecuteNonQuery();

                    String strDrop = "DROP DATABASE " + tenCoSoDuLieu;
                    sqlCommandObj.CommandText = strDrop;
                    sqlCommandObj.ExecuteNonQuery();
                }

                myConnection.ChangeDatabase("Master");

                strPhucHoi = "RESTORE DATABASE " + tenCoSoDuLieu + " FROM DISK = '" +
                    tenDuongDan + "'";
                sqlCommandObj.CommandText = strPhucHoi;
                sqlCommandObj.ExecuteNonQuery();

                XtraMessageBox.Show("Phục hồi thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                myConnection.ChangeDatabase(tenCoSoDuLieu);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                myConnection.Close();
            }
        }

        public static bool CoSoDuLieuCoTonTai(string tenCoSoDuLieu, SqlConnection myConnection)
        {
            try
            {
                myConnection.ChangeDatabase(tenCoSoDuLieu);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
             
            }
        }

    }
}
